import numpy as np
from src.config import config as cfg

print("this is Ball.py")


# 食物类
# 方法： 放置/移除
# 点以25为单位
class Ball:
    # 初始化各种需要的属性 [开始时默认向右/身体块x5]
    def __init__(self):
        self.direction = (1, -1)
        # x,y是600*600像素里，30乘以30个格子的下标
        self.i = 15
        self.j = 15
        self.fps = 2
        self.width = cfg.UNIT

    def reset(self):
        self.__init__()

    # 移动！
    def change_radian(self):
        # todo
        print(1)

    # 移动！
    def move(self):
        self.i += self.direction[0]
        self.j += self.direction[1]
        if self.i < 0 or self.i >= 20 or self.j < 0 or self.j >= 20:
            self.i -= self.direction[0]
            self.j -= self.direction[1]
            print(1)

    # 死亡判断
    def is_lost(self):
        # 撞墙
        return self.y > 400

    def check_crash(self, snake, brick, game_info):
        next_i, next_j = self.i + self.direction[0], self.j + self.direction[1]

        # 撞墙检测，如果撞墙，其他不用检测
        is_crash_wall = self.check_wall(next_i, next_j)
        if is_crash_wall:
            return True

        # 撞蛇检测，如果撞蛇了（即需要变向，则需要再判断）
        # is_crash_snake = self.check_snake(next_i, next_j, snake)
        is_crash_snake = self.check_snake2(snake)
        if is_crash_snake:
            return True

        # 撞砖块检测，如果撞砖块了（即需要变向，则需要再判断）
        is_crash_brick = self.check_brick(brick, game_info)
        if is_crash_brick:
            return True

        return False

    def check_wall(self, next_i, next_j):
        direction = self.direction
        # 撞墙
        is_crash_wall = False
        if next_i >= cfg.SCREEN_X / self.width or next_i < 0:
            is_crash_wall = True
            self.direction = np.multiply(direction, (-1, 1))
            # self.direction = np.multiply(direction, (-1, 1))
        if next_j >= cfg.SCREEN_Y / self.width or next_j < 0:
            is_crash_wall = True
            self.direction = np.multiply(direction, (1, -1))
        return is_crash_wall

    def check_snake2(self, snake):
        # 获取方向上的临格，和下一个格
        next_x_direction = self.i + self.direction[0], self.j
        next_y_direction = self.i, self.j + self.direction[1]
        next_xy_direction = self.i + self.direction[0], self.j + self.direction[1]

        # 特殊情况，蛇比球运动快，ball检测的时候，已经和蛇重叠，则不算碰撞（毕竟总不能停在蛇肚子里）
        if (self.i, self.j) in snake.body:
            return False

        # 如果临格不空，则需要变向（x方向）
        is_crash_snake = False
        if next_x_direction in snake.body:
            self.direction = np.multiply(self.direction, (-1, 1))
            is_crash_snake = True
        # 如果临格不空，则需要变向（y方向）
        if next_y_direction in snake.body:
            self.direction = np.multiply(self.direction, (1, -1))
            is_crash_snake = True
        # 如果临格均空，且下个位置不空，则需要变向（xy方向都变）
        if next_x_direction not in snake.body and next_y_direction not in snake.body and next_xy_direction in snake.body:
            self.direction = np.multiply(self.direction, (-1, -1))
            is_crash_snake = True

        return is_crash_snake

    def check_brick(self, brick, game_info):
        # 获取方向上的临格，和下一个格
        next_x_direction = self.i + self.direction[0], self.j
        next_y_direction = self.i, self.j + self.direction[1]
        next_xy_direction = self.i + self.direction[0], self.j + self.direction[1]
        x = brick.bricks[next_x_direction[1]][next_x_direction[0]]
        y = brick.bricks[next_y_direction[1]][next_y_direction[0]]
        xy = brick.bricks[next_xy_direction[1]][next_xy_direction[0]]

        # 如果临格不空，则需要变向（x方向）
        is_crash_brick = False
        if x != 0:
            brick.bricks[next_x_direction[1]][next_x_direction[0]] = 0
            self.direction = np.multiply(self.direction, (-1, 1))
            is_crash_brick = True
            game_info.add_score(5)
        # 如果临格不空，则需要变向（y方向）
        if y != 0:
            brick.bricks[next_y_direction[1]][next_y_direction[0]] = 0
            self.direction = np.multiply(self.direction, (1, -1))
            is_crash_brick = True
            game_info.add_score(5)
        # 如果临格均空，且下个位置不空，则需要变向（xy方向都变）
        if x == 0 and y == 0 and xy != 0:
            brick.bricks[next_xy_direction[1]][next_xy_direction[0]] = 0
            self.direction = np.multiply(self.direction, (-1, -1))
            is_crash_brick = True
            game_info.add_score(5)
        return is_crash_brick
